// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/testing/exec/util/assert_query_builder.h>
#include <pollux/testing/exec/util/operator_test_base.h>
#include <pollux/plan/plan_builder.h>

using namespace kumo::pollux;
using exec::test::AssertQueryBuilder;

namespace kumo::pollux::exec::test {

class ValuesTest : public OperatorTestBase {
 protected:
  void TearDown() override {
    input_.reset();
    input2_.reset();
    OperatorTestBase::TearDown();
  }

  // Sample row vectors.
  RowVectorPtr input_{make_row_vector({
      make_flat_vector<int32_t>({0, 1, 2, 3, 5}),
      make_flat_vector<StringView>({"a", "b", "c", "d", "e"}),
      make_flat_vector<float>({0.1, 2.3, 4.5, 6.7, 8.9}),
  })};

  RowVectorPtr input2_{make_row_vector({
      make_flat_vector<int32_t>({6, 7}),
      make_flat_vector<StringView>({"f", "g"}),
      make_flat_vector<float>({10.11, 12.13}),
  })};
};

TEST_F(ValuesTest, empty) {
  // Base case: no vectors.
  AssertQueryBuilder(PlanBuilder().values({}).planNode()).assertEmptyResults();

  // Empty input vector.
  auto emptyInput = make_row_vector({});
  AssertQueryBuilder(PlanBuilder().values({emptyInput}).planNode())
      .assertEmptyResults();

  // Many empty vectors as input.
  AssertQueryBuilder(
      PlanBuilder().values({emptyInput, emptyInput, emptyInput}).planNode())
      .assertEmptyResults();
}

TEST_F(ValuesTest, simple) {
  // Single vector in, single vector out.
  AssertQueryBuilder(PlanBuilder().values({input_}).planNode())
      .assertResults({input_});

  // 3 vectors in, 3 vectors out.
  AssertQueryBuilder(PlanBuilder().values({input_, input_, input_}).planNode())
      .assertResults({input_, input_, input_});

  // Combine two different vectors.
  AssertQueryBuilder(
      PlanBuilder().values({input_, input2_, input2_, input_}).planNode())
      .assertResults({input_, input2_, input2_, input_});
}

TEST_F(ValuesTest, valuesWithParallelism) {
  // Parallelism true but single thread.
  AssertQueryBuilder(PlanBuilder().values({input_}, true).planNode())
      .assertResults({input_});

  // 5 threads.
  AssertQueryBuilder(PlanBuilder().values({input_}, true).planNode())
      .maxDrivers(5)
      .assertResults({input_, input_, input_, input_, input_});
}

TEST_F(ValuesTest, valuesWithRepeat) {
  // Single vectors in with repeat, many vectors out.
  AssertQueryBuilder(PlanBuilder().values({input_}, false, 2).planNode())
      .assertResults(std::vector<RowVectorPtr>{input_, input_});

  AssertQueryBuilder(PlanBuilder().values({input_}, false, 7).planNode())
      .assertResults({input_, input_, input_, input_, input_, input_, input_});

  // Zero repeats.
  AssertQueryBuilder(PlanBuilder().values({}, false, 0).planNode())
      .assertEmptyResults();
  AssertQueryBuilder(PlanBuilder().values({input_}, false, 0).planNode())
      .assertEmptyResults();

  // Try repeating with 2 different row vectors.
  AssertQueryBuilder(
      PlanBuilder().values({input_, input2_}, false, 3).planNode())
      .assertResults({input_, input2_, input_, input2_, input_, input2_});
}

TEST_F(ValuesTest, valuesWithRepeatAndParallelism) {
  // Two threads repeating twice each buffer.
  AssertQueryBuilder(
      PlanBuilder().values({input_, input2_}, true, 2).planNode())
      .maxDrivers(2)
      .assertResults(
          {input_, input2_, input_, input2_, input_, input2_, input_, input2_});
}

} // namespace kumo::pollux::exec::test
